// 各个城区 option
var area = ['上城区', '下城区', '江干区', '拱墅区', '西湖区', '滨江区', '萧山区', '余杭区', '桐庐县', '淳安县', '建德市', '富阳区', '临安区'];
// a-e 各类总收集数
var a = 0,
    b = 0,
    c = 0,
    d = 0,
    e = 0;
// $.ajax({
//     url: "../../../Message/yearTypeCollect",
//     dataType: "json",
//     type: "post",
//     success: function(res) {
//         a = res.data[1];
//         $("#grx").html(a.toLocaleString());
//         b = res.data[2];
//         $("#ssx").html(b.toLocaleString());
//         c = res.data[4];
//         $("#blx").html(c.toLocaleString());
//         d = res.data[3];
//         $("#ywx").html(d.toLocaleString());
//         $("#hxx").html(0);
//         e = res.data[0];
//         $("#total").html(e.toLocaleString());
//     }
// })

$("#grx").html(1234567);
$("#ssx").html(1234567);
$("#ywx").html(1234567);
$("#hxx").html(1234567);
$("#blx").html(1234567);
$("#dai").html(1234567);

$("#ping").html(1234567);

$("#total").html(123456789);
// 图表颜色
var colors = [
    '#3366cc', '#dc3912', '#ff9900', '#109618', '#990099', '#0099c6', '#dd4477', '#66aa00',
    '#b82e2e', '#316395', '#994499', '#22aa99', '#aaaa11', '#6633cc', '#e67300', '#8b0707',
    '#651067', '#329262', '#5574a6', '#3b3eac'
]

function getNY() {
    var d = new Date()
    var nian = d.getFullYear()
    $('#nian').html(nian)
    var yue = d.getMonth() + 1
    if (yue < 10) {
        yue = '0' + yue
    }
    $('#yue').html(yue)
}


/* 各区县本年收集总量排名*/
var mychart = echarts.init($("#collm")[0]);
$.ajax({
        url: "../../../Message/yearRegionCollect",
        dataType: "json",
        type: "post",
        success: function(res) {
            var areaname = new Array(13);
            var weight = new Array(13);
            var j = res.data.length - 1;
            for (var i = 0; i < res.data.length; i++) {
                switch (res.data[i].region) {
                    case 0:
                        areaname[j - i] = "上城区";
                        break;
                    case 1:
                        areaname[j - i] = "下城区";
                        break;
                    case 2:
                        areaname[j - i] = "江干区";
                        break;
                    case 3:
                        areaname[j - i] = "拱墅区";
                        break;
                    case 4:
                        areaname[j - i] = "西湖区";
                        break;
                    case 5:
                        areaname[j - i] = "滨江区";
                        break;
                    case 6:
                        areaname[j - i] = "萧山区";
                        break;
                    case 7:
                        areaname[j - i] = "余杭区";
                        break;
                    case 8:
                        areaname[j - i] = "桐庐县";
                        break;
                    case 9:
                        areaname[j - i] = "淳安县";
                        break;
                    case 10:
                        areaname[j - i] = "建德市";
                        break;
                    case 11:
                        areaname[j - i] = "富阳区";
                        break;
                    case 12:
                        areaname[j - i] = "临安区";
                        break;
                    default:
                        break;
                };
                weight[j - i] = parseFloat(res.data[i].weight);
            }
            var option = {
                color: ["#0998f6"],
                legend: {
                    data: ["收集量"],
                    show: false,
                    top: "bottom"
                },
                grid: {
                    bottom: "1%",
                    top: "1%",
                    left: "10",
                    right: "25%",
                    containLabel: true
                },
                xAxis: {
                    type: "value",
                    boundaryGap: [0, 0.01],
                    show: false,
                    label: {
                        normal: {
                            show: true,
                            position: "inside"
                        }
                    }
                },
                yAxis: {
                    type: "category",
                    data: areaname,
                    axisLabel: {
                        show: true,
                        textStyle: {
                            color: "#fff",
                            fontWeight: "bold"
                        }
                    }
                },
                series: [{
                    name: "收集量",
                    type: "bar",
                    data: weight,
                    barWidth: 20,
                    label: {
                        normal: {
                            show: true,
                            position: 'top',
                            color: "white",
                            fontSize: 13,
                            fontWeight: "bold"
                        }
                    },
                    itemStyle: {
                        normal: {
                            color: function(params) {
                                if (params.value == weight[weight.length - 1]) {
                                    return "#f08519";
                                } else if (params.value == weight[weight.length - 2]) {
                                    return "#acce22";
                                } else if (params.value == weight[weight.length - 3]) {
                                    return "#f00";
                                } else {
                                    return "#0998f6";
                                }
                            }
                        }
                    },
                    barCategoryGap: "10%"
                }]
            };
            mychart.setOption(option);
        }
    })
    // 各个城区 option
    /* 今年各月杭州市收集总量(Kg)*/
var mychart1 = echarts.init($("#colly")[0]);
$.ajax({
        url: "../../../Message/monthCollect",
        dataType: "json",
        type: "post",
        success: function(res) {
            var mondata = res.data;
            var ms = new Date().getMonth();
            var month;
            switch (ms) {
                case 0:
                    month = ['1月'];
                    break;
                case 1:
                    month = ['1月', '2月'];
                    break;
                case 2:
                    month = ['1月', '2月', '3月'];
                    break;
                case 3:
                    month = ['1月', '2月', '3月', '4月'];
                    break;
                case 4:
                    month = ['1月', '2月', '3月', '4月', '5月'];
                    break;
                case 5:
                    month = ['1月', '2月', '3月', '4月', '5月', '6月'];
                    break;
                case 6:
                    month = ['1月', '2月', '3月', '4月', '5月', '6月', '7月'];
                    break;
                case 7:
                    month = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月'];
                    break;
                case 8:
                    month = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月'];
                    break;
                case 9:
                    month = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月'];
                    break;
                case 10:
                    month = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月'];
                    break;
                case 11:
                    month = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
                    break;
                default:
                    break;
            }
            var option1 = {
                color: ["#0998f6"],
                legend: {
                    data: ["收集量"],
                    top: "top",
                    show: false,
                    textStyle: {
                        color: "#fff"
                    }
                },
                grid: {
                    left: "-47",
                    right: "10",
                    top: "20",
                    bottom: "1%",
                    containLabel: true
                },
                yAxis: {
                    type: "value",
                    boundaryGap: [0, 0.01],
                    show: true,
                    label: {
                        normal: {
                            show: true,
                            position: "inside"
                        }
                    },
                    axisLabel: {
                        show: false
                    },
                    splitLine: {
                        show: false
                    },
                    axisLine: {
                        show: false
                    }
                },
                xAxis: {
                    type: "category",
                    data: month,
                    axisLabel: {
                        show: true,
                        textStyle: {
                            color: "#fff",
                            fontSize: 15,
                            fontWeight: "bold"
                        }
                    }
                },
                series: [{
                    name: "收集量",
                    type: "bar",
                    data: mondata,
                    barWidth: 20,
                    itemStyle: {
                        normal: {
                            color: function(params) {
                                if (params.value == Math.max.apply(null, mondata)) {
                                    return "#f08519";
                                } else if (params.value == Math.min.apply(null, mondata)) {
                                    return "#acce22";
                                } else {
                                    return "#0998f6";
                                }
                            }
                        }
                    }
                }, {
                    name: "客户数",
                    type: "line",
                    smooth: true,
                    symbol: "circle", //拐点样式
                    symbolSize: 8,
                    data: mondata,
                    label: {
                        normal: {
                            show: true,
                            position: "outside",
                            fontSize: 12,
                            fontWeight: "bold",
                            color: function(params) {
                                if (params.value == Math.max.apply(null, mondata)) {
                                    return "#f08519";
                                } else if (params.value == Math.min.apply(null, mondata)) {
                                    return "#acce22";
                                } else {
                                    return "#fff";
                                }
                            }
                        }
                    },
                    lineStyle: {
                        normal: {
                            width: 3,
                            color: "#27ff55"
                        }
                    },
                    itemStyle: {
                        normal: {
                            color: function(params) {
                                if (params.value == Math.max.apply(null, mondata)) {
                                    return "#f08519";
                                } else if (params.value == Math.min.apply(null, mondata)) {
                                    return "#acce22";
                                } else {
                                    return "#fff";
                                }
                            }
                        }
                    }
                }]
            };
            mychart1.setOption(option1);
        }
    })
    /**
     * 各区县昨日收集数据
     */
var dayData = [];
$.ajax({
        url: "../../../Message/regionDayCollect",
        dataType: "json",
        type: "post",
        success: function(res) {
            dayData = res.data;
            var k = 0;
            var m1 = 0,
                m2 = 0,
                m3 = 0,
                m4 = 0;
            for (var d = 0; d < res.data.length; d++) {
                if (m1 < res.data[d][0]) {
                    m1 = parseInt(res.data[d][0]) + 50
                }
                if (m2 < res.data[d][1]) {
                    m2 = parseInt(res.data[d][1]) + 20
                }
                if (m3 < res.data[d][3]) {
                    m3 = parseInt(res.data[d][3]) + 5
                }
                if (m4 < res.data[d][2]) {
                    m4 = parseInt(res.data[d][2]) + 10
                }
            }

            if (m1 == 0) { m1 = m1 + 50 }
            if (m2 == 0) { m2 = m2 + 20 };
            if (m3 == 0) { m3 = m3 + 10 };
            if (m4 == 0) { m4 = m4 + 5 };
            var desc = new Array(4);
            for (var i = 0; i < res.data[k].length; i++) {
                desc[i] = res.data[k][res.data[k].length - 1 - i]
            }
            var option6 = {
                tooltip: {},
                legend: {
                    data: ["收集量"],
                    show: false
                },
                radar: {
                    // shape: 'circle',
                    name: {
                        textStyle: {
                            color: "#fff",
                            fontSize: 15,
                            fontWeight: "bold",
                            borderRadius: 3
                        }
                    },
                    center: ['50%', '50%'],
                    radius: '60%',
                    indicator: [{
                        name: "病理性",
                        max: m3
                    }, {
                        name: "药物性",
                        max: m4
                    }, {
                        name: "损伤性",
                        max: m2
                    }, {
                        name: "感染性",
                        max: m1
                    }],
                    splitArea: {
                        show: true,
                        areaStyle: {
                            color: ["#133d32", "#7DB5DA"] // 图表背景网格的颜色
                        }
                    }
                },

                series: [{
                    name: "收集量",
                    type: "radar",

                    // areaStyle: {normal: {}},
                    data: [{
                        value: desc,
                        name: "收集量",
                        symbolSize: 6,
                        label: {
                            normal: {
                                show: true,
                                color: "#fff",
                                fontSize: 15,
                                formatter: function(params) {
                                    return params.value;
                                }
                            }
                        }
                    }],
                    areaStyle: {
                        normal: {
                            color: "#64acba"
                        }
                    },
                    lineStyle: {
                        normal: {
                            color: "yellow",
                            width: 2
                        }
                    }
                }]
            };

            var option7 = {
                color: ["#0998f6"],
                legend: {
                    data: ["收集量"],
                    show: false,
                    top: "bottom"
                },
                grid: {
                    left: "3%",
                    right: "19%",
                    bottom: "1%",
                    top: "1%",
                    containLabel: true
                },
                xAxis: {
                    type: "value",
                    boundaryGap: [0, 0.01],
                    show: false,
                    label: {
                        normal: {
                            show: true,
                            position: "inside"
                        }
                    }
                },
                yAxis: {
                    type: "category",
                    data: ["病理性", "药物性", "损伤性", "感染性"],
                    axisLabel: {
                        show: true,
                        textStyle: {
                            color: "#fff",
                            fontSize: 15,
                            fontWeight: "bold"
                        }
                    }
                },
                series: [{
                    name: "收集量",
                    type: "bar",
                    barWidth: 30,
                    data: desc,
                    label: {
                        normal: {
                            show: true,
                            color: "white",
                            position: "right",
                            fontSize: 14,
                            fontWeight: "bold"
                        }
                    },
                    barCategoryGap: "10%"
                }]
            };
            mychart4.setOption(option6);
            mychart5.setOption(option7);

            setInterval(function() {
                $("#region").html(area[k]);
                $("#region1").html(area[k]);
                $("#region2").html(area[k]);
                if (k < area.length) {
                    if (k == 0) {
                        $("#img6").css("display", "none");
                        $("#img0").css("display", "block");
                    } else {
                        var j = k - 1;
                        $("#img" + j).css("display", "none");
                        $("#img" + k).css("display", "block");
                    }
                    var desc = new Array(4);
                    for (var i = 0; i < res.data[k].length; i++) {
                        desc[i] = res.data[k][res.data[k].length - 1 - i]
                    }
                    var option4 = {
                        tooltip: {},
                        legend: {
                            data: ["收集量"],
                            show: false
                        },
                        radar: {
                            // shape: 'circle',
                            name: {
                                textStyle: {
                                    color: "#fff",
                                    fontSize: 15,
                                    fontWeight: "bold",
                                    borderRadius: 3
                                }
                            },
                            center: ['50%', '50%'],
                            indicator: [{
                                name: "病理性",
                                max: m3
                            }, {
                                name: "药物性",
                                max: m4
                            }, {
                                name: "损伤性",
                                max: m2
                            }, {
                                name: "感染性",
                                max: m1
                            }],
                            splitArea: {
                                show: true,
                                areaStyle: {
                                    color: ["#133d32", "#7DB5DA"] // 图表背景网格的颜色
                                }
                            }
                        },

                        series: [{
                            name: "收集量",
                            type: "radar",

                            // areaStyle: {normal: {}},
                            data: [{
                                value: desc,
                                name: "收集量",
                                symbolSize: 6,
                                label: {
                                    normal: {
                                        show: true,
                                        color: "#fff",
                                        fontSize: 15,
                                        formatter: function(params) {
                                            return params.value;
                                        }
                                    }
                                }
                            }],
                            areaStyle: {
                                normal: {
                                    color: "#64acba"
                                }
                            },
                            lineStyle: {
                                normal: {
                                    color: "yellow",
                                    width: 2
                                }
                            }
                        }]
                    };

                    var option5 = {
                        color: ["#0998f6"],
                        legend: {
                            data: ["收集量"],
                            show: false,
                            top: "bottom"
                        },
                        grid: {
                            left: "3%",
                            right: "19%",
                            bottom: "1%",
                            top: "1%",
                            containLabel: true
                        },
                        xAxis: {
                            type: "value",
                            boundaryGap: [0, 0.01],
                            show: false,
                            label: {
                                normal: {
                                    show: true,
                                    position: "inside"
                                }
                            }
                        },
                        yAxis: {
                            type: "category",
                            data: ["病理性", "药物性", "损伤性", "感染性"],
                            axisLabel: {
                                show: true,
                                textStyle: {
                                    color: "#fff",
                                    fontSize: 15,
                                    fontWeight: "bold"
                                }
                            }
                        },
                        series: [{
                            name: "收集量",
                            type: "bar",
                            barWidth: 30,
                            data: desc,
                            label: {
                                normal: {
                                    show: true,
                                    color: "white",
                                    position: "right",
                                    fontSize: 14,
                                    fontWeight: "bold"
                                }
                            },
                            barCategoryGap: "10%"
                        }]
                    };
                    k++;
                    mychart4.setOption(option4);
                    mychart5.setOption(option5);
                } else {
                    k = 0;
                }
            }, 30000);
        }
    })
    /*实时收集数据 
     */
$.ajax({
    url: "../../../Message/nowCollect",
    dataType: "json",
    type: "post",
    success: function(res) {
        var str = "";
        var q = res.data;
        for (var t = 0; t < res.data.length; t++) {
            str += "<tr>";
            str += "<td>" + q[t].hospname + "</td>";
            str += "<td>" + q[t].area + "</td>";
            str += "<td>" + q[t].weight + "</td>";
            str += "<td>" + q[t].ctime + "</td>";
            str += "</tr>";
        }
        $("#tt").prepend(str);
    }
})
$.ajax({
    url: "../../../Message/hospCollect",
    dataType: "json",
    type: "post",
    success: function(res) {
        var j = res.data;
        for (var i = 0; i < 4; i++) {
            var $h = "#h" + i;
            var charts = echarts.init($($h)[0]);
            var op = lb(j[i][4], j[i][2], j[i][3], j[i][1], j[i][0]);
            charts.setOption(op);
        }
    }
})

function lb(name, qt, lwq, guil, garbage) {
    var option3 = {
        backgroundColor: "black",
        color: ["#EE9572", "#FFB5C5", "#859DAD", "#39B8D8"],
        legend: {
            data: ["感染性", "损伤性", "病理性", "药物性"],
            top: "top",
            left: "right",
            textStyle: {
                color: "#fff"
            },
            orient: "vertical"
        },
        grid: {
            top: "5",
            bottom: "10",
            left: "left",
            containLabel: true
        },
        yAxis: {
            type: "value",
            show: true,
            axisLabel: {
                color: "#fff",
                normal: {
                    show: true,
                    position: "inside"
                }
            },

            splitLine: {
                show: true
            },
            axisLine: {
                color: "#fff",
                show: true
            }
        },
        xAxis: {
            type: "category",
            data: [name],
            axisLabel: {
                show: true,
                textStyle: {
                    color: "#fff",
                    fontSize: 15,
                    fontWeight: "bold"
                }
            }
        },
        series: [{
            name: "药物性",
            type: "bar",
            stack: "总量",
            barWidth: 85,
            label: {
                normal: {
                    show: true,
                    position: "inside",
                    fontSize: 13,
                    fontWeight: "bold"
                }
            },
            data: [qt]
        }, {
            name: "病理性",
            type: "bar",
            stack: "总量",
            barWidth: 85,
            label: {
                normal: {
                    show: true,
                    position: "inside",
                    fontSize: 13,
                    fontWeight: "bold"
                }
            },
            data: [guil]
        }, {
            name: "损伤性",
            type: "bar",
            stack: "总量",
            barWidth: 85,
            label: {
                normal: {
                    show: true,
                    position: "inside",
                    fontSize: 13,
                    fontWeight: "bold"
                }
            },
            data: [lwq]
        }, {
            name: "感染性",
            type: "bar",
            stack: "总量",
            barWidth: 85,
            label: {
                normal: {
                    show: true,
                    position: "inside",
                    fontSize: 13,
                    fontWeight: "bold"
                }
            },
            data: [garbage]
        }]
    };
    return option3;
}